﻿/**
 * $RCSfile: RequestUtils.java,v $
 * $Revision: 1.1 $
 * $Date: 2009/06/15 08:23:32 $
 *
 * Copyright (C) 2003 ICSS, Inc. All rights reserved.
 *
 * This software is the proprietary information of ICSS, Inc.
 * Use is subject to license terms.
 */
package net.sapfree.display.tags.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;
import org.apache.commons.beanutils.PropertyUtils;
 
 
public class RequestUtils {

	private static Map scopes = new HashMap();
	static {
		scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
		scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
		scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
		scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
	}

	/**
	 * Commons Logging instance.
	 */
	protected static Logger log = Logger.getLogger(RequestUtils.class.getClass());
 

	public static Map getParameterMap(ServletRequest request) {
		Map paramMap = new HashMap();
		Enumeration paramEnu = request.getParameterNames();
		while (paramEnu.hasMoreElements()) {
			String paramName = (String) paramEnu.nextElement();
			paramMap.put(paramName, request.getParameter(paramName));
		}
		return paramMap;
	}
	
	/**
	 * Locate and return the specified bean, from an optionally specified scope,
	 * in the specified page context. If no such bean is found, return
	 * <code>null</code> instead. If an exception is thrown, it will have
	 * already been saved via a call to <code>saveException()</code>.
	 * 
	 * @param pageContext
	 *            Page context to be searched
	 * @param name
	 *            Name of the bean to be retrieved
	 * @param scopeName
	 *            Scope to be searched (page, request, session, application) or
	 *            <code>null</code> to use <code>findAttribute()</code>
	 *            instead
	 * @return JavaBean in the specified page context
	 * @exception JspException
	 *                if an invalid scope name is requested
	 */
	public static Object lookup(PageContext pageContext, String name,
			String scopeName) {
		try {
			if (scopeName == null) {
				return pageContext.findAttribute(name);
			} else {
				return pageContext.getAttribute(name, getScope(scopeName));
			}
		} catch (Exception e) {
			return null;
		}

	}
	
	public static int getScope(String scopeName) throws JspException {
		Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
		if (scope == null) {
			throw new JspException("获取生命周期失败");
		}
		return scope.intValue();
	}
	
	
	
	public static Object lookup(PageContext pageContext, String name,
			String property, String scope) throws JspException {

		// Look up the requested bean, and return if requested
		Object bean = lookup(pageContext, name, scope);
		if (bean == null) {
			if (scope == null) {
				ResponseUtils.write(pageContext, "找不到生命周期");
			} else {
				ResponseUtils.write(pageContext, "找不到BEAN");
			}
			return null;
		}

		if (property == null) {
			return bean;
		}

		return getProperty(pageContext, bean, property);

	}

	public static Object getProperty(PageContext pageContext, Object bean,
			String property) throws JspException {
		// Locate and return the specified property
		try {
			return PropertyUtils.getProperty(bean, property);
		} catch (IllegalAccessException e) {
			ResponseUtils.write(pageContext, "错误的入口");
			return null;
		} catch (InvocationTargetException e) {
			Throwable t = e.getTargetException();
			if (t == null) {
				t = e;
			}
			ResponseUtils.write(pageContext, "得到属性失败");
			return null;

		} catch (NoSuchMethodException e) {
			ResponseUtils.write(pageContext, "得到属性失败，没有这个方法"); 
			return null;
		}
	}
}